#############################################################################
# Copyright (c) 2009 Dmitry Grushin <dgrushin@gmail.com>.
#
# This file is part of clupower (cluster power manager utility)
#
# Clupower is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Clupower is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Clupower.  If not, see <http://www.gnu.org/licenses/>.
#
# Contributors:
#    Dmitry Grushin <dgrushin@gmail.com> - initial API and implementation
#############################################################################

require 'client'
require 'server'
require 'header'
require 'test'

# Align description to 76 chars. Name +3
SHUTDOWN = ["stop", "Stops the power manager. The manager will try\n" + 
    "           to power on all the nodes."]
EXIT = ["exit", "Stops the power manager and leave powered off\n" +
    "           nodes as is, only clear torque offline status.\n" +
    "           Do not remove job holds if any."]
START = ["start", "Start the power manager"]
DAEMON = ["daemon", "Start the power manager as a daemon"]
TEST = ["test", "Test installation of the power manager"]
ALGO = ["heuristics", "Lists available power manager heuristics"]
RELOAD = ["reload", "Reload configuration file"]
STATUS = ["status", "Report server status"]
NODESTATUS = ["nodestatus", "Report cluster node status"]

def usage
  puts "Usage: #{$0} <command>"
  puts 
  puts "  Available commands:"
  for cmd in [START, SHUTDOWN, DAEMON, EXIT, TEST, STATUS, NODESTATUS, ALGO, RELOAD]
    puts "    #{cmd[0]} - #{cmd[1]}"
  end
end

begin
  puts HEADER
  puts
  if ARGV.size >= 1 then
    command = ARGV[0]
    args = ARGV.drop(1)
    client = CPClient.new
    case
    when command == SHUTDOWN[0] || command == EXIT[0]
      client.shutdown(command == EXIT[0])
    when command == START[0] || command == DAEMON[0]
      puts "Starting cluster power manager. See log file #{Settings.instance.get_main_log_path} for detailed status messages."
      CPServer.new.run(command == DAEMON[0])
    when command == TEST[0]
      CPTest.new.test
    when command == ALGO[0]
      puts "  Available heuristics (* means the default):"
      PMA.get_subclasses.collect { |a| " #{a[1].to_s == Settings.instance.get('DEF_ALGO') ? '*' : ' '}  #{a[1].to_s} - #{a[1].get_desc}"}.each {|a| puts a  }
    when command == RELOAD[0]
      puts "  Reloading configuration: #{client.reload}"
    when command == STATUS[0]
      puts "  Server status: #{client.status}"
    when command == NODESTATUS[0]
      unless args.empty? 
        puts "  Node #{args[0]}: #{client.node_status(args[0])}"
      else
        puts "  Node name required"
      end
    else
      usage
    end
  else
    usage
  end
rescue CPError => e
  puts "Error: #{e.get_trace}"
  #puts ""
  #puts "Error trace: #{e.backtrace}" if e.kind_of?(CPInitError)
  exit(1)
end